package com.worldtvs.online.database;

import java.util.ArrayList;
import java.util.LinkedHashMap;

import android.content.ContentValues;
import com.worldtvs.online.entity.InfoAds;
import com.worldtvs.online.entity.InfoAlbum;
import com.worldtvs.online.entity.InfoTotalAlbum;
import com.worldtvs.online.entity.InfoVideo;



public class DBFunction {
private DBAdapter db;
	
	public DBFunction(DBAdapter db) {
		this.db = db;
	}
	
	public void dropDB() {
		try {
			db.delete(MySQLiteHelper.TABLE_SLIDE, "1=1");
			db.delete(MySQLiteHelper.TABLE_ALBUM, "1=1");
			db.delete(MySQLiteHelper.TABLE_VIDEO, "1=1");
		
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void insertDataSlide(String id,String id_youtube,String title,String image,int type){
		try {
			if(!db.contains(MySQLiteHelper.TABLE_SLIDE, MySQLiteHelper.ID_SLIDE, MySQLiteHelper.ID_SLIDE + "="+id+"")){
				ContentValues value = new ContentValues();
				value.put(MySQLiteHelper.ID_SLIDE, id);
				value.put(MySQLiteHelper.ID_YOUTUBE_SLIDE, id_youtube);
				value.put(MySQLiteHelper.TITLE_SLIDE, title);
				value.put(MySQLiteHelper.IMAGE_SLIDE, image);
				value.put(MySQLiteHelper.TYPE_SLIDE, type);
				db.insert(MySQLiteHelper.TABLE_SLIDE, value);
			}
		} catch (Exception e) {
		}
	}
	public void insertDataTotalAlbum(String id ,String title,int count_album){
		try {
			if(!db.contains(MySQLiteHelper.TABLE_TOTAL_ALBUM, MySQLiteHelper.ID_TOTAL_ALBUM, MySQLiteHelper.ID_TOTAL_ALBUM + "="+id+"")){
				ContentValues value = new ContentValues();
				value.put(MySQLiteHelper.ID_TOTAL_ALBUM, id);
				value.put(MySQLiteHelper.TITLE_TOTAL_ALBUM, title);
				value.put(MySQLiteHelper.COUT_TOTAL_ALBUM, count_album);
				db.insert(MySQLiteHelper.TABLE_TOTAL_ALBUM, value);
			}
		} catch (Exception e) {
		}
	}
	
	public void insertDataVideo(String id,String id_youtube,String title,String image,String vew,String duration,String id_cat){
		try {
			if(!db.contains(MySQLiteHelper.TABLE_VIDEO, MySQLiteHelper.ID_VIDEO, MySQLiteHelper.ID_VIDEO + "="+id+"")){
				ContentValues value = new ContentValues();
				value.put(MySQLiteHelper.ID_VIDEO, id);
				value.put(MySQLiteHelper.ID_YOUTUBE_VIDEO, id_youtube);
				value.put(MySQLiteHelper.TITLE_VIDEO, title);
				value.put(MySQLiteHelper.IMAGE_VIDEO, image);
				
				value.put(MySQLiteHelper.VEW_VIDEO, vew);
				value.put(MySQLiteHelper.DURATION_VIDEO, duration);
				value.put(MySQLiteHelper.ID_CAT_VIDEO, id_cat);
				db.insert(MySQLiteHelper.TABLE_VIDEO, value);
			}
		} catch (Exception e) {
		}
	}
	public void insertDataVideoList(String id_key,String id,String id_youtube,String title,String image,String vew,String duration,String id_cat){
		try {
			if(!db.contains(MySQLiteHelper.TABLE_VIDEO_LIST, MySQLiteHelper.ID_VIDEO_LIST, MySQLiteHelper.ID_VIDEO_LIST + "="+id+"")){
				ContentValues value = new ContentValues();
				value.put(MySQLiteHelper.ID_KEY_CHA, id_key);
				value.put(MySQLiteHelper.ID_VIDEO_LIST, id);
				value.put(MySQLiteHelper.ID_YOUTUBE_VIDEO_LIST, id_youtube);
				value.put(MySQLiteHelper.TITLE_VIDEO_LIST, title);
				value.put(MySQLiteHelper.IMAGE_VIDEO_LIST, image);
				
				value.put(MySQLiteHelper.VEW_VIDEO_LIST, vew);
				value.put(MySQLiteHelper.DURATION_VIDEO_LIST, duration);
				value.put(MySQLiteHelper.ID_CAT_VIDEO_LIST, id_cat);
				db.insert(MySQLiteHelper.TABLE_VIDEO_LIST, value);
			}
		} catch (Exception e) {
		}
	}
	public void insertDataAlbum(String id,String title,String image,String count_album,String cat_id_album,String playlist){
		try {
			if(!db.contains(MySQLiteHelper.TABLE_ALBUM, MySQLiteHelper.ID_ALBUM, MySQLiteHelper.ID_ALBUM + "="+id+"")){
				ContentValues value = new ContentValues();
				value.put(MySQLiteHelper.ID_ALBUM, id);
				value.put(MySQLiteHelper.TITLE_ALBUM, title);
				value.put(MySQLiteHelper.IMAGE_ALBUM, image);
				value.put(MySQLiteHelper.COUNT_ALBUM, count_album);
				value.put(MySQLiteHelper.CAT_ID_ALBUM, cat_id_album);
				value.put(MySQLiteHelper.PLAYLISTS_ALBUM, playlist);
				db.insert(MySQLiteHelper.TABLE_ALBUM, value);
				
			}
		} catch (Exception e) {
		}
	}
	public ArrayList<InfoVideo> getVideo (){
		ArrayList<InfoVideo> obInfoVideos = new ArrayList<InfoVideo>();
		try {
			ArrayList<LinkedHashMap<String, Object>> records = db.fetchAll(MySQLiteHelper.TABLE_VIDEO, null, null);
				for (int i = 0; i < records.size(); i++) {

					InfoVideo mInfoVideo = new InfoVideo();
					LinkedHashMap<String, Object> row = records.get(i);

					mInfoVideo.setVid((String)row.get(MySQLiteHelper.ID_VIDEO));
					mInfoVideo.setvName((String)row.get(MySQLiteHelper.TITLE_VIDEO));
					mInfoVideo.setvYoutube((String)row.get(MySQLiteHelper.ID_YOUTUBE_VIDEO));
					mInfoVideo.setvAvatar((String)row.get(MySQLiteHelper.IMAGE_VIDEO));
					obInfoVideos.add(mInfoVideo);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return obInfoVideos;
	}
	public ArrayList<InfoAds> getSlide (){
		ArrayList<InfoAds> obInfoVideos = new ArrayList<InfoAds>();
		try {
			ArrayList<LinkedHashMap<String, Object>> records = db.fetchAll(MySQLiteHelper.TABLE_SLIDE, null, null);
				for (int i = 0; i < records.size(); i++) {

					InfoAds mInfoVideo = new InfoAds();
					LinkedHashMap<String, Object> row = records.get(i);

					mInfoVideo.setId((String)row.get(MySQLiteHelper.ID_SLIDE));
					mInfoVideo.setTitle((String)row.get(MySQLiteHelper.TITLE_SLIDE));
					mInfoVideo.setIdYoutube((String)row.get(MySQLiteHelper.ID_YOUTUBE_SLIDE));
					mInfoVideo.setImage((String)row.get(MySQLiteHelper.IMAGE_SLIDE));
					obInfoVideos.add(mInfoVideo);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return obInfoVideos;
	}
	public ArrayList<InfoAlbum> getAlbum (){
		ArrayList<InfoAlbum> obInfoVideos = new ArrayList<InfoAlbum>();
		
		try {
			ArrayList<LinkedHashMap<String, Object>> records = db.fetchAll(MySQLiteHelper.TABLE_ALBUM, null, null);
				for (int i = 0; i < records.size(); i++) {

					InfoAlbum mInfoVideo = new InfoAlbum();
					LinkedHashMap<String, Object> row = records.get(i);

					mInfoVideo.setAid((String)row.get(MySQLiteHelper.ID_ALBUM));
					mInfoVideo.setaName((String)row.get(MySQLiteHelper.TITLE_ALBUM));
					mInfoVideo.setaAvatar((String)row.get(MySQLiteHelper.IMAGE_ALBUM));
					mInfoVideo.setCat_id((String)row.get(MySQLiteHelper.CAT_ID_ALBUM));
					mInfoVideo.setPlaylist((String)row.get(MySQLiteHelper.PLAYLISTS_ALBUM));
					obInfoVideos.add(mInfoVideo);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return obInfoVideos;
	}
	
	public ArrayList<InfoTotalAlbum> getTotalAlbum (){
		ArrayList<InfoTotalAlbum> obInfoVideos = new ArrayList<InfoTotalAlbum>();
		try {
			ArrayList<LinkedHashMap<String, Object>> records = db.fetchAll(MySQLiteHelper.TABLE_TOTAL_ALBUM, null, null);
				for (int i = 0; i < records.size(); i++) {

					InfoTotalAlbum mInfoVideo = new InfoTotalAlbum();
					LinkedHashMap<String, Object> row = records.get(i);

					mInfoVideo.setId((String)row.get(MySQLiteHelper.ID_TOTAL_ALBUM));
					mInfoVideo.setTitle((String)row.get(MySQLiteHelper.TITLE_TOTAL_ALBUM));
					mInfoVideo.setCount_album((String)row.get(MySQLiteHelper.COUT_TOTAL_ALBUM));
					obInfoVideos.add(mInfoVideo);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return obInfoVideos;
	}
	public void updateVideoList(String id_key_cha){
		ContentValues values = new ContentValues();
		values.put(MySQLiteHelper.ID_KEY_CHA,id_key_cha);
		db.update(MySQLiteHelper.TABLE_VIDEO_LIST, values, MySQLiteHelper.ID_KEY_CHA + " = "+id_key_cha);
	}


	
}
